﻿/**
 * 分阶段加载本地数据缓存
 */
app.loading.set(0, () => cache.load());
app.loaded.set(0, () => {
    let userInfo = localCache.global.get('userInfo');
    if (userInfo && userInfo.user && userInfo.user.name) {
        $(".header .user-info a.nav-link:first").html('[ ' + userInfo.user.name + ' ]');
        var $menu = $('.header .user-menu');
        if ($.com.isArray(userInfo.resources)) {
            userInfo.resources = userInfo.resources.sort((x, y) => x.code >= y.code ? 1 : -1);
            for (var i = 0; i < userInfo.resources.length; i++) {
                let resource = userInfo.resources[i];
                if (resource.children && $.com.isArray(resource.children)) {
                    let sub = '<li role="presentation" class="dropdown-hover">';
                    sub += `<a class="dropdown-toggle" data-toggle="dropdown" href="${resource.content}" aria-haspopup="true" aria-expanded="false">`;
                    sub += resource.title + `<span class="caret"></span></a>`;
                    sub += `<ul class="dropdown-menu">`;
                    for (var index = 0; index < resource.children.length; index++) {
                        var child = resource.children[index];
                        sub += `<li class="nav-item"><a class="nav-link text-dark" href="${child.content}">${child.title}</a></li>`;
                    }

                    sub += `</ul>`;
                    sub += `</li>`;
                    $menu.append(sub);
                } else {
                    $menu.append(`<li class="nav-item"><a class="nav-link text-dark" href="${resource.content}">${resource.title}</a></li>`);
                }
            }
        }

        if (!app.communicator) {
            app.communicator = new Communicator({
                host: "wss://dcs.devonline.cn/dcs",
                user: userInfo.user.userName,
                client: "auth",
                onReceive: message => {
                    app.info(`收到了来自用户: ${message.sender} 从: ${message.from} 发来的消息: \n"${message.content}", \n发送时间: ${$.com.dateTimeFormatter(message.sendTime)}`);
                },
                onNotice: message => {
                    app.info(`收到了来自用户: ${message.sender} 从: ${message.from} 发来的通知: "${message.content}", \n发送时间: ${$.com.dateTimeFormatter(message.sendTime)}`);
                },
                onWarning: message => {
                    app.warning(`收到了来自用户: ${message.sender} 从: ${message.from} 发来的告警消息: "${message.content}", \n发送时间: ${$.com.dateTimeFormatter(message.sendTime)}`);
                },
                onError: message => {
                    app.error(`收到了来自用户: ${message.sender} 从: ${message.from} 发来的错误消息: "${message.content}", \n发送时间: ${$.com.dateTimeFormatter(message.sendTime)}`);
                }
            });
            app.communicator.start();
        }
    }
});

/**
 * 本地数据缓存
 */
var cache = new (class _Cache_ {
    constructor() {
        this.parameters = [];
    }

    set(data) {
        this.parameters = data;
        this.root = this.parameters.get(x => x.key == "root", null, true);
        this.top = this.parameters.get(x => x.parent && x.parent.key === 'root');
        if ($.com.isArray(this.top)) {
            for (var index = 0; index < this.top.length; index++) {
                let element = this.top[index];
                this[element.key] = this.parameters.get(x => x.parent && x.parent.key == element.key);
            }
        }
    }

    load() {
        return Promise.resolve()
            .then(() => {
                let userInfo = localCache.global.get('userInfo');
                if (!userInfo) {
                    return axios.get('/api/account/getUserInfo')
                        .then(resp => resp.data)
                        .then(data => {
                            if (data && data.user && data.resources) {
                                localCache.global.set('userInfo', data);
                            }
                        });
                }
            })
            .then(() => {
                let parameters = localCache.global.get('parameters');
                if ($.com.isArray(parameters)) {
                    this.set(parameters);
                } else {
                    return axios.get('/api/parameters?$select=id,index,key,value,text,parentid&$expand=parent($select=id,key)&$orderby=index')
                        .then(resp => resp.data)
                        .then(data => {
                            data = $.com.dataAdapter(data);
                            if ($.com.isArray(data)) {
                                localCache.global.set('parameters', data)
                                this.set(data);
                            }
                        });
                }
            });
    }
})();